package com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.OrderTenementListItemBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnClick;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.order.TenementOrderBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/14.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.adapter.orderAdapter
 */

public class OrderTenementListAdapter extends RecyclerView.Adapter<OrderTenementListAdapter.BindingHolder> {
    Context context;
    OrderTenementListItemBinding binding;
    LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;
    OnClick onClick;
    List<TenementOrderBean.OrderlistBean> list=new ArrayList<>();


    public OrderTenementListAdapter(Context context,List<TenementOrderBean.OrderlistBean> list) {
        this.context = context;
        this.list=list;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public OrderTenementListAdapter.BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        binding= DataBindingUtil.inflate(inflater, R.layout.order_tenement_list_item,parent,false);
        OrderTenementListAdapter.BindingHolder holder = new OrderTenementListAdapter.BindingHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(final OrderTenementListAdapter.BindingHolder holder, final int position) {
        TenementOrderBean.OrderlistBean bean=list.get(position);
        holder.binding.tenementType.setText(bean.getOrderTitle());
        holder.binding.tenementPayState.setText(bean.getPayStatusName());
        holder.binding.serviceTime.setText(bean.getBillTime());
        holder.binding.feeDetail.setText(bean.getUnitCost()+"元"+"*"+bean.getBillTime());
        holder.binding.tenementOrderTime.setText(bean.getCreateAt());
        holder.binding.orderTenementItemPrice.setText("￥"+bean.getOrderPrice());

        if (bean.getPayStatusName().equals("已支付")){
            holder.binding.btnTenementPay.setVisibility(View.GONE);
        }else if(bean.getPayStatusName().equals("未支付")){
            holder.binding.btnTenementPay.setVisibility(View.VISIBLE);
        }

        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
        }

        binding.btnTenementPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onClick(v,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class BindingHolder extends RecyclerView.ViewHolder {
        OrderTenementListItemBinding binding;

        public OrderTenementListItemBinding getBinding() {
            return binding;
        }

        public void setBinding(OrderTenementListItemBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnclickListener(OnClick onClick){
        this.onClick=onClick;
    }
}
